package com.meitianhui.convenitenbuy.main.presenter;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.meitianhui.convenitenbuy.App;
import com.meitianhui.convenitenbuy.Constants;
import com.meitianhui.convenitenbuy.base.BaseResponse;
import com.meitianhui.convenitenbuy.config.InfoProvider;
import com.meitianhui.convenitenbuy.http.ResponseObserver;
import com.meitianhui.convenitenbuy.main.contract.SplashContract;
import com.meitianhui.convenitenbuy.main.http.MainApiDataSource;
import com.meitianhui.convenitenbuy.main.model.bean.AppToken;
import com.meitianhui.convenitenbuy.main.model.bean.UserToken;
import com.meitianhui.convenitenbuy.main.ui.SplashActivity;

import javax.inject.Inject;


public class SplashPresenter implements SplashContract.Presenter {

    private SplashActivity context;
    private final SplashContract.View view;

    @Inject
    public SplashPresenter(SplashContract.View view, SplashActivity context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void getAppToken() {
        MainApiDataSource.getInstance()
                .getAppToken(Constants.APP_ID, Constants.PRIVATE_KEY)
                .subscribe(new ResponseObserver<BaseResponse<AppToken>>(App.getInstance()) {
                    @Override
                    public void onSuccess(BaseResponse<AppToken> appToken) {
                        view.getTokenSuccess(appToken.data);
                    }

                    @Override
                    public void onFail(int code, String msg) {
                        super.onFail(code, msg);
                        view.getTokenFail(code, msg);
                    }
                });
    }

    @Override
    public void isReLoginHyphenate() {
        try {
            if (!EMClient.getInstance().isLoggedInBefore() || !EMClient.getInstance().isConnected()) {
                MainApiDataSource.getInstance()
                        .isReLoginHypenate(InfoProvider.getUserToken(App.getInstance()).getUser_token())
                        .subscribe(new ResponseObserver<BaseResponse<UserToken>>(App.getInstance()) {
                            @Override
                            public void onSuccess(BaseResponse<UserToken> response) {
                                view.isReLoginHyphenateSuccess();
                            }

                            @Override
                            public void onFailure(Throwable e) {
                                super.onFailure(e);
                                view.isReLoginHyphenateFail();
                            }
                        });
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void loginHyphenate() {
        EMClient.getInstance().login(
                InfoProvider.getHyphenateInfo(App.getInstance()).getIm_user_id(),
                InfoProvider.getHyphenateInfo(App.getInstance()).getIm_user_id(),
                new EMCallBack() {
                    @Override
                    public void onSuccess() {
                        EMClient.getInstance().groupManager().loadAllGroups();
                        EMClient.getInstance().chatManager().loadAllConversations();
                    }

                    @Override
                    public void onProgress(int progress, String status) {
                    }

                    @Override
                    public void onError(final int code, final String message) {
                    }
                });
    }

    @Override
    public void attachView(Object view) {
    }

    @Override
    public void detachView() {
    }

}
